#include "noborderwidget.h"
#include "showwidget.h"

#include <QVBoxLayout>
#include <QDebug>
#include <QMouseEvent>
#include <QMessageBox>
#include <QDebug>
#include <QGraphicsDropShadowEffect>
#include <QPainter>
#include <QStyleOption>

#if defined (Q_OS_WINDOWS)
// Windows环境
#include <qt_windows.h>
#include <windows.h>
#include <windowsx.h>
#endif


NoborderWidget::NoborderWidget(QWidget *parent)
    : QWidget(parent)
{
    // 设置无边框
    this->setWindowFlags(Qt::Window | Qt::FramelessWindowHint | Qt::WindowMinMaxButtonsHint);

    // 不显示在任务栏
//    this->setWindowFlag(Qt::Drawer);
//    this->setWindowFlag(Qt::FramelessWindowHint);

    this->setObjectName("box");

    qDebug() << "wind" << this->objectName();
//    resize(600, 400);
    setMinimumSize(400,300);
    move(400, 300);

    initGUI();
}

NoborderWidget::~NoborderWidget()
{

}

void NoborderWidget::initGUI()
{
    // 设置主窗口
    QVBoxLayout *mlayout = new QVBoxLayout(this);
    mainwidget = new QWidget(this);
    mlayout->addWidget(mainwidget);
    mlayout->setContentsMargins(10,10,10,10);
    setLayout(mlayout);
    mainwidget->setStyleSheet("QWidget{background-color:gray;border-radius:10px;}");

    shadeGUI(); // 窗口阴影 透明

    editwidget = new ShowWidget(mainwidget);
    titlebar = new TitleBar(mainwidget);

    QVBoxLayout *layout = new QVBoxLayout(mainwidget);
    layout->addWidget(titlebar);
    layout->addWidget(editwidget);

    layout->setContentsMargins(0,0,0,0);
    layout->setSpacing(0);
    mainwidget->setLayout(layout);

    connect(titlebar, &TitleBar::signalClose, this, &NoborderWidget::closeCallback);
}

void NoborderWidget::shadeGUI()
{
    /* 设置窗口阴影 */
    // 设置窗体透明
    this->setAttribute(Qt::WA_TranslucentBackground, true);

    QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(mainwidget);
    // 设置阴影距离
    shadow->setOffset(0, 0);
    // 设置阴影颜色
    shadow->setColor(QColor("#7f00ff"));
    // 设置阴影区域
    shadow->setBlurRadius(30);
    // 给顶层widget设置阴影
    mainwidget->setGraphicsEffect(shadow);
}

void NoborderWidget::mouseDoubleClickEvent(QMouseEvent *event)
{

}

void NoborderWidget::paintEvent(QPaintEvent *event)
{
    QStyleOption opt;
    opt.init(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}

#if defined (Q_OS_WINDOWS)
    // Windows环境
bool NoborderWidget::nativeEvent(const QByteArray &eventType, void *message, long *result)
{
    /*支持代码缩放的代码*/

    MSG* param = static_cast<MSG*>(message);

    switch (param->message)
    {
        case WM_NCHITTEST:
        {
            int nX = GET_X_LPARAM(param->lParam) - this->geometry().x();
            int nY = GET_Y_LPARAM(param->lParam) - this->geometry().y();

            /*if (childAt(nX, nY) != nullptr)
                return QWidget::nativeEvent(eventType, message, result);*/

            if (nX > m_nBorderWidth && nX < this->width() - m_nBorderWidth &&
                nY > m_nBorderWidth && nY < this->height() - m_nBorderWidth)
            {
                if (childAt(nX, nY) != nullptr)
                    return QWidget::nativeEvent(eventType, message, result);
            }

            if ((nX > 0) && (nX < m_nBorderWidth))
                *result = HTLEFT;

            if ((nX > this->width() - m_nBorderWidth) && (nX < this->width()))
                *result = HTRIGHT;

            if ((nY > 0) && (nY < m_nBorderWidth))
                *result = HTTOP;

            if ((nY > this->height() - m_nBorderWidth) && (nY < this->height()))
                *result = HTBOTTOM;

            if ((nX > 0) && (nX < m_nBorderWidth) && (nY > 0)
                && (nY < m_nBorderWidth))
                *result = HTTOPLEFT;

            if ((nX > this->width() - m_nBorderWidth) && (nX < this->width())
                && (nY > 0) && (nY < m_nBorderWidth))
                *result = HTTOPRIGHT;

            if ((nX > 0) && (nX < m_nBorderWidth)
                && (nY > this->height() - m_nBorderWidth) && (nY < this->height()))
                *result = HTBOTTOMLEFT;

            if ((nX > this->width() - m_nBorderWidth) && (nX < this->width())
                && (nY > this->height() - m_nBorderWidth) && (nY < this->height()))
                *result = HTBOTTOMRIGHT;

            return true;
        }
    }

    return false;
}
#endif

void NoborderWidget::closeCallback()
{
//    QMessageBox::information(this, "close", "close widget");
    close();
}

